import { kaoqinUrl } from "./config.js";
import Axios from "axios";
import moment from 'moment';

//Axios.defaults.baseURL = baseUrl;

export let getOriginalNameById = (manager, departmentId) => {
    return (
        Axios.post(kaoqinUrl + "AttendanceSys/getOriginalNameById", {
            manager: manager,
            departmentId: departmentId
        })
    );
}

export let getAllOriginalListByClass = (groupId) => {
    return (
        Axios.get(kaoqinUrl + "AttendanceSys/getAllOriginalListByClass", {
            params: {
                originalNameId: groupId
            }
        })
    );
}

export let deleteOriginalListByIds = (groupDetailData) => {
    let ids = groupDetailData.filter(v => {
        if(v.checked) return v;
    }).map(val => val.id).join(",");
    return (
        Axios.post(kaoqinUrl + "AttendanceSys/deleteOriginalListByIds", {
            ids: ids
        })
    );
}

export let deleteOriginalName = (groupId) => {
    return (
        Axios.post(kaoqinUrl + "AttendanceSys/deleteOriginalName", {
            id: groupId
        })
    );
}

export let insertOriginalName = (manager, name, departmentId, type) => {
    return (
        Axios.post(kaoqinUrl + "AttendanceSys/insertOriginalName", {
            manager: manager, 
            name: name,
            departmentId: departmentId,
            type: type
        })
    );
}

export let updateOriginalName = (manager, name, groupId) => {
    return (
        Axios.post(kaoqinUrl + "AttendanceSys/updateOriginalName", {
            manager: manager, 
            name: name,
            id: groupId
        })
    );
}

export let insertOriginalList = (name, account, groupId) => {
    return (
        Axios.post(kaoqinUrl + "AttendanceSys/insertOriginalList", [{
            name: name,
            account: account,
            groupNameId: groupId
        }])
    );
}

export let getBluetoothList = (userId) => {
    return (
        Axios.post(kaoqinUrl + "AttendanceSys/getBluetoothDeviceByUserId", {
            userId: userId
        })
    );
}

export let deleteBluetoothDevice = (id) => {
    return (
        Axios.post(kaoqinUrl + "AttendanceSys/deleteBluetoothDevice", {
            id: id,
            invalid: 1
        })
    );
}

export let addBluetoothDevice = (formVal) => {
    return (
        Axios.post(kaoqinUrl + "AttendanceSys/addBluetoothDevice", {
            username: formVal.username,
            major: formVal.major,
            minor: formVal.minor,
        })
    );
}

export let updateBluetoothDevice = (id, formVal) => {
    return (
        Axios.post(kaoqinUrl + "AttendanceSys/updateBluetoothDevice", {
            id: id,
            major: formVal.major,
            minor: formVal.minor,
            invalid: 0
        })
    );
}

export let importOriginalList = (formData, groupNameId) => {
    formData.append("groupNameId", groupNameId)
    return (
        Axios({
            method: 'post',
            url: kaoqinUrl + 'AttendanceSys/importOriginalList',
            headers: {"Content-Type": "multipart/form-data"},
            processData: false,
            data: formData
        })
    );
}

export let getMeetingListByManager = (userId) => {
    return (
        Axios.post(kaoqinUrl + "AttendanceSys/getMeetingListByManager", {
            manager: userId
        })
    );
}

export let deleteMeetingById = (id) => {
    return (
        Axios.post(kaoqinUrl + "AttendanceSys/deleteMeetingById", {
            id: id
        })
    );
}

export let getStaticInfo = (userId) => {
    return (
        Axios.post(kaoqinUrl + "AttendanceSys/getStaticInfo", {
            manager: userId
        })
    );
}

export let addMeeting = (userId, type, other) => {
    return (
        Axios.post(kaoqinUrl + "AttendanceSys/addMeeting", {
            name: other.name,
            manager: userId,
            groupNameId: other.groupNameId,
            blueToothIdList: other.blueToothIdList.join(','),
            type: type,
            startDate: other.date == null ? null : moment(other.date).format("YYYY-MM-DD"),
            startTime: other.date == null ? null : moment(other.time).format("HH:mm")
        })
    );
}

export let updateMeetingById = (id, userId, other) => {
    return (
        Axios.post(kaoqinUrl +"AttendanceSys/updateMeetingById", {
            id: id, 
            name: other.name,
            manager: userId,
            groupNameId: other.groupNameId,
            blueToothIdList: other.blueToothIdList.join(','),
            type: other.type,
            startDate: other.date == null ? null : moment(other.date).format("YYYY-MM-DD"),
            startTime: other.date == null ? null : moment(other.time).format("HH:mm")
        })
    );
}

export let getMeetingInfoByType = (userId) => {
    return (
        Axios.post(kaoqinUrl + "AttendanceSys/getMeetingInfoByType", {
            manager: userId
        })
    );
}

export let getCheckCreateDate = (meetingId) => {
    return (
        Axios.post(kaoqinUrl + "AttendanceSys/getCheckCreateDate", {
            id: meetingId
        })
    );
}

export let getCheckCondition = (meetingId, date) => {
    return (
        Axios.post(kaoqinUrl + "AttendanceSys/getCheckCondition", {
            id: meetingId,
            createDate: date
        })
    );
}

export let getBluetoothDeviceByDepartment = (userId) => {
    return (
        Axios.post(kaoqinUrl + "AttendanceSys/getBluetoothDeviceByDepartment", {
            userId: userId
        })
    );
}

export let saveAsNewGroup = (userId, departmentId, groupName, ids, type) => {
    return (
        Axios.post(kaoqinUrl + "AttendanceSys/saveAsNewGroup", {
            ids: ids,
            name: groupName,
            manager: userId,
            type: type,
            departmentId: departmentId
        })
    );
}